package com.un.ebs.memorial.controller;


import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.memorial.enums.MemorialResourceTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.un.ebs.memorial.service.MemorialResourceService;
import com.un.ebs.memorial.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2020-03-12
 */
@RestController
@RequestMapping("/memorialResource")
@Api(value = "memorial-resource", tags = "")
public class MemorialResourceController {

    @Autowired
    private MemorialResourceService service;

    @PostMapping("upload")
    @ApiOperation(value = "upload", notes = "上传资源")
    public WebResult save(@RequestParam MemorialResourceTypeEnum type, MultipartFile file) {
        List<MultipartFile> files = new ArrayList<>();
        files.add(file);
        service.add(type, files);
        return WebUtils.success();
    }

    @PostMapping("batchUpload")
    @ApiOperation(value = "batchUpload", notes = "添加")
    public WebResult save(MultipartHttpServletRequest request) {
        MultipartHttpServletRequest req = request;
        String type = req.getParameter("type");
        if (StringUtils.isNullOrEmpty(type)) {
            throw new BusinessException("type 值不能为空。");
        }

        List<MultipartFile> files = req.getFiles("file");
        if (MemorialResourceTypeEnum.BG_Image.getDesc().equals(type)) {
            service.add(MemorialResourceTypeEnum.BG_Image, files);
        } else if (MemorialResourceTypeEnum.BG_Music.equals(type)) {
            service.add(MemorialResourceTypeEnum.BG_Music, files);
        } else {
            throw new BusinessException("type 取值不正确。");
        }
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getById", notes = "根据主键获取单个 MemorialResource")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "list", notes = "")
    public WebResult list() {
        return WebUtils.success();
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "changeSort", notes = "修改 MemorialResource 的序号")
    public WebResult changeSort(@RequestParam String id, @RequestParam int index) {
        service.changeSort(id, index);
        return WebUtils.success();
    }

}